package com.dmzapp.api.service;

import java.util.List;

import com.dmzapp.api.cache.redis.RedisClient;
import com.dmzapp.api.db.AppDao;
import com.dmzapp.api.vo.IncomeRecord;
import com.dmzapp.api.vo.OutRecord;
/**
 * 该类型里面的操作都是以下流程：
 * 先从redis中取，如果没有key，则到数据库中取，如果有值的话将值放入redis中，并返回
 * @author walterpei
 *
 */
public class RedisAndDBService {
    

    public static void inserIncomeDetail(IncomeRecord incomeRecord) {
        AppDao.getInstance().insertIncomeHistory(incomeRecord);
        RedisClient.getInstance().addUserincome(incomeRecord);
    }
    
/*    *//**
     * 暂时不用cache，用户支出列表是一个不频繁的操作
     * @param username
     * @param record
     *//*
    public static void insertUserOutcomeDetail(String username, OutRecord record) {
        //判断是否在redis中存在用户的信息，如果没有的话，去数据库中获取，最后在加入新的out明细
        List<OutRecord> outlist = RedisClient.getInstance().getUserOutcomelist("username");  
        if (outlist == null || outlist.isEmpty()) {
            // 如果开始没有redis数据，将数据库中可能存在的数据导入到redis中
            outlist = AppDao.getInstance().getOutcomelistByusername(username);
            
            if (outlist != null && !outlist.isEmpty()) {
                RedisClient.getInstance().setUseroutomceList(outlist);
            }
        }
        // 最后将新的数据加入到redis中
        RedisClient.getInstance().addUseroutcome(record);
    }*/
    
    
    public static List<IncomeRecord> getUserInfomelist(String username) {
        List<IncomeRecord> incomelist = RedisClient.getInstance().getUserIncomelist(username);
        if (incomelist == null || incomelist.isEmpty()) {
            incomelist = AppDao.getInstance().getIncomelistByusername(username);
            if (incomelist != null && !incomelist.isEmpty()) {
                RedisClient.getInstance().setUserinomceList(incomelist);
            }
        }
        return incomelist;
    }
    

    /**
     * 直接查询数据库
     * @param username
     * @return
     */
    public static List<OutRecord> getUseroutcomeList(String username) {
        
//        List<OutRecord> outlist = RedisClient.getInstance().getUserOutcomelist(username);
//
//        if (outlist == null || outlist.isEmpty()) {
//            outlist = AppDao.getInstance().getOutcomelistByusername(username);
//            if (outlist != null && !outlist.isEmpty()) {
//                RedisClient.getInstance().setUseroutomceList(outlist);
//            }
//        }
        List<OutRecord> outlist = AppDao.getInstance().getOutcomelistByusername(username);
        return outlist;
    }
    
    // =============================== 余额查询start
    public static String getUserAmount(String username) {
        String amountStr;
        if ((amountStr = RedisClient.getInstance().getUserAmount(username)) == null) {
            
            List<IncomeRecord> incomelist = RedisAndDBService.getUserInfomelist(username);
            amountStr = RedisClient.getInstance().setUserAmount(username, incomelist);

        }
        
        if (amountStr == null) {
            amountStr = "0";
        }
        
        return amountStr;
    }
    
    public static String getUserRest(String username){
        String restStr = null;
        if ((restStr = RedisClient.getInstance().getUserRest(username)) == null) {
            List<IncomeRecord> inlist = AppDao.getInstance().getIncomelistByusername(username);
            List<OutRecord> outlist = AppDao.getInstance().getOutcomelistByusername(username);
            restStr = RedisClient.getInstance().setUserRest(username, inlist, outlist);
        }
        
        if (restStr == null) {
            restStr = "0";
        }
        return restStr;
    }
    
    public static void addUserRestAndAmount(String username, String enduserIncome) {
        List<IncomeRecord> inlist = null;
        if (RedisClient.getInstance().getUserRest(username) == null) {
            inlist = AppDao.getInstance().getIncomelistByusername(username);
            List<OutRecord> outlist = AppDao.getInstance().getOutcomelistByusername(username);
            RedisClient.getInstance().setUserRest(username, inlist, outlist);
        }
        
        if (RedisClient.getInstance().getUserAmount(username) == null) {
            if(inlist == null){
                inlist = AppDao.getInstance().getIncomelistByusername(username);
            }
            RedisClient.getInstance().setUserAmount(username, inlist);
        }
        
        RedisClient.getInstance().addUserRest(username, enduserIncome);
        RedisClient.getInstance().addUserAmount(username, enduserIncome);
    }
    
    public static String reduceUserRest(String username, String amount){
        
        if (RedisClient.getInstance().getUserRest(username) == null) {
            List<IncomeRecord> inlist = AppDao.getInstance().getIncomelistByusername(username);
            List<OutRecord> outlist = AppDao.getInstance().getOutcomelistByusername(username);
            RedisClient.getInstance().setUserRest(username, inlist, outlist);
        }
        
        return RedisClient.getInstance().reduceUserRest(username, amount);
    }

    // =============================== 余额查询end
    

}
